import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car') || '[]')
let defaultBedNo = ''
try {
  if (localStorage.bedNo) {
    defaultBedNo = localStorage.bedNo
  }
} catch (e) {
  // alert('出错了')
  console.log('出错了')
}
export default new Vuex.Store({
  state: {
    bedNo: defaultBedNo,
    cardNo: '',
    car: car,
    personinfo: {},
    scroll: {},
    belongDpname: '',
    orderPath: '',
    painLevel: '',
    idCard: null,
    paId: '',
    payInfo: {},
    orderSerialNumber: ''
  },
  actions: { // 获取数据
    changeBedNo: function (ctx, bedNo) { // ctx 指的是上下文 bedNo 接受值
      ctx.commit('changeBedNo', bedNo) // commit 两个参数 自定义事件名称 和数据
    },
    changePersonInfo: function (ctx, personinfo) {
      ctx.commit('changePersonInfo', personinfo)
    }
  },
  mutations: {
    changeBedNo: function (state, bedNo) {
      state.bedNo = bedNo
    },
    changePersonInfo: function (state, personinfo) {
      state.personinfo = personinfo
    },
    getcardNo: function (state, cardNo) {
      state.cardNo = cardNo
    },
    addToCar (state, foodList) {
      var flag = false // 给个标识符，默认为false
      state.car.some(item => { // 循环数组car里的每个对象
        if (item.type_id === foodList.type_id && item.goods_name === foodList.goods_name) { // 判断如果car里有对象并且和传过来的foodList里的id相同就把foodList的数量赋值给car里对应的对象
          item.num = foodList.num
          flag = true // 并且让标识符为true
          return true
        }
      })
      if (!flag) { // 如果标识符为true，就将3传递过来的foodList放在car里
        state.car.push(foodList)
      }
      localStorage.setItem('car', JSON.stringify(state.car)) // 将car存放在本地
    },
    subToCar (state, foodList) { // 同上
      state.car.some(item => {
        if (item.type_id === foodList.type_id && item.goods_name === foodList.goods_name) {
          item.num = foodList.num
          return true
        }
      })
      if (foodList.hasOwnProperty('currentNum')) { // 如果标识符为true，就将3传递过来的foodList放在car里
        for (let index = 0; index < state.car.length; index++) {
          if (state.car[index] && foodList.type_id === state.car[index].id) {
            state.car.splice(index, 1)
          }
        }
      }
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updataToCar (state) { // 返回上一页清空car
      state.car = []
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    ScrollValue (state, scroll) {
      state.scroll = scroll
    },
    belongDpname (state, belongDpname) {
      state.belongDpname = belongDpname
    },
    orderPathClick (state, pathname) {
      state.orderPath = pathname
    },
    getPainLevel (state, painlevel) {
      state.painLevel = painlevel
    },
    getIdCard (state, idCard) {
      state.idCard = idCard
    },
    getPaId (state, paid) {
      state.paId = paid
    },
    getPayInfo (state, payinfo) {
      state.payInfo = payinfo
    },
    getOrderSerialNumber (state, number) {
      state.orderSerialNumber = number
    }
  },
  getters: {
    getAllCount: (state) => (id) => { // 计算左边菜单栏的数量
      var c = 0
      state.car.forEach((item) => {
        // console.log(id)
        // console.log(item.type_id)
        if (item.type_id === id) { // 根据菜单栏的id与菜品的typeid相同，就相加对应的数量
          c += item.num
        }
      })
      return c
    },
    getAllPrice (state) { // 计算总价格
      var allprice = 0
      let allsprice = 0
      state.car.forEach(item => {
        Math.formatFloat = function (f, digit) {
          var m = Math.pow(10, digit)
          return parseInt(f * m, 10) / m
        }
        allsprice += Math.formatFloat(item.num, 10) * Math.formatFloat(item.price, 10) // 每一项的数量 x 每一项的单价，
        allprice = Math.formatFloat(allsprice, 10).toFixed(2) // 再相加每一项的总价格
      })
      return allprice
    }
  }
})
